package  
{
	import flash.geom.Rectangle;
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxObject;
	import org.flixel.FlxPoint;
	import org.flixel.FlxRect;
	import org.flixel.FlxSave;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	import org.flixel.FlxText;
	import org.flixel.FlxTilemap;
	import org.flixel.FlxU;
	import org.flixel.plugin.photonstorm.*;
	/**
	 * ...
	 * @author Billy Kaifer
	 */
	public class PlayState extends FlxState
	{
		[Embed(source = "../media/blue_bullet.png")]public var blue_B:Class;
		[Embed(source = "../media/background.png")]public var background:Class;
		
		private var gun:FlxWeapon;
		private var bg:FlxSprite;
		
		protected static const PLAYER_RUN_SPEED:int = 400;
		private const START_X:int = 20;
		private const START_Y:int = (FlxG.height / 2) - 20;
		private const BULLET_SPEED:int = 600;
		
		private var currentWeapon:int;
		private var weapons:Array = new Array();
		//private var hud:HUD;
		
		override public function create():void {
			Registry.player = new Player(START_X, START_Y, this);
			Registry.player.health = 100;
			//Registry.enemies = new EnemyManager(Knight);
			Registry.currentLevel = 1;
			Registry.scoreThisLevel = 0;
			Registry.scoreThisGame = 0;
			Registry.enemiesKilledThisLevel = 0;
			Registry.enemiesKilledThisGame = 0;
		
			Registry.shotsFiredThisLevel = 0;
			Registry.shotsFiredThisGame = 0;
			Registry.enemiesHitThisLevel = 0;
			Registry.enemiesHitThisGame = 0;
			Registry.positionOffset = new FlxPoint(40, 20);
			
			Registry.init();
			
			// Add weapons
			weapons.push(new SpreadShot(1));
			weapons.push(new SpreadShot(3,25,200));
			currentWeapon = 0;
			
			
			// World Boundry
			FlxG.worldBounds.width = FlxG.width;
			FlxG.worldBounds.height = FlxG.height;
			FlxG.worldBounds.x = 0;
			FlxG.worldBounds.y = 0;
			FlxG.camera.setBounds(0, 0, FlxG.width, FlxG.height);
			
			if (FlxG.getPlugin(FlxControl) == null) {
				FlxG.addPlugin(new FlxControl);
			}
			if (FlxG.getPlugin(FlxScrollZone) == null)
			{
				FlxG.addPlugin(new FlxScrollZone);
			}
			
			// Scrolling Background
			bg = new FlxSprite(0, 0, background);
			FlxScrollZone.add(bg, new Rectangle(0, 0, bg.width, bg.height), -4, 0, true);
			
			// Player Controls
			FlxControl.create(Registry.player, FlxControlHandler.MOVEMENT_INSTANT, FlxControlHandler.STOPPING_INSTANT, 1, false,false);
			FlxControl.player1.setWASDControl(true, true, true, true);
			FlxControl.player1.setMovementSpeed(PLAYER_RUN_SPEED, PLAYER_RUN_SPEED, PLAYER_RUN_SPEED, PLAYER_RUN_SPEED, 0, 0);

			
			add(bg);
			add(Registry.hud);
			//add(Registry.enemies);
			add(Registry.knights);
			add(Registry.fx);
			for each (var wep:FlxWeapon in weapons) {
				add(wep.group);
			}
			add(Registry.knightWep.group);
			add(Registry.player);
		}
		
		override public function update():void {
			super.update();
			FlxG.camera.update();
			FlxG.collide(Registry.hud, Registry.player);
			//FlxG.overlap(weapons[currentWeapon].group, Registry.enemies, Registry.enemies.shotHitEnemy);
			//FlxG.overlap(Registry.enemies, Registry.player, Registry.player.collidesWithEnemy);
			FlxG.overlap(weapons[currentWeapon].group, Registry.knights, Registry.knights.shotHitEnemy);
			FlxG.overlap(Registry.knights, Registry.player, Registry.player.collidesWithEnemy);
			FlxG.overlap(Registry.knightBulletGroup, Registry.player, Registry.knights.enemyHitPlayer);
			
			if (Registry.player.y < 0) {
				Registry.player.y = 0;
			}else if ((Registry.player.y + Registry.player.height) > FlxG.height) {
				Registry.player.y = (FlxG.height - Registry.player.height);
			}
			if (Registry.player.x < 0) {
				Registry.player.x = 0;
			} else if ((Registry.player.x + Registry.player.width) > FlxG.width) {
				Registry.player.x = (FlxG.width - Registry.player.width);
			}
			
			if (FlxG.keys.justPressed("ONE")) {
				swapWeapon(0);
			}
			if (FlxG.keys.justPressed("TWO")) {
				swapWeapon(1);
			}
			
			if (FlxG.mouse.pressed()) {
				weapons[currentWeapon].fire();
			}
			
			if (Registry.player.health <= 0) {
				FlxG.switchState(new GameOverState);
			}
		}
		
		override public function destroy():void {
			FlxControl.clear();
			FlxScrollZone.clear();
			super.destroy();
		}
		
		private function swapWeapon(wep:int):void {
			currentWeapon = wep;
		}
		
	}

}